﻿using System;
using System.Collections.Generic;
using System.Text;
using Yuebon.Car.Dtos;
using Yuebon.Car.IServices;
using Yuebon.Car.Models;
using Yuebon.Commons.IoC;
using Yuebon.Commons.Models;
using System.Linq;
using Yuebon.Commons;
using Yuebon.Shop.IServices;
using System.Threading.Tasks;
using Yuebon.CMS.Models;
using Yuebon.CMS.IServices;

namespace Yuebon.Car.Application
{
    public class AutoDetailsApp
    {
        IAutoDetailsService service = IoCContainer.Resolve<IAutoDetailsService>();
        IAutoBrandService serviceAutoBrand = IoCContainer.Resolve<IAutoBrandService>();
        IMembersService serviceMembers = IoCContainer.Resolve<IMembersService>();
        IReferralsService serviceReferrals = IoCContainer.Resolve<IReferralsService>();
        IAlbumsService albumsService= IoCContainer.Resolve<IAlbumsService>();
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">是否降序</param>
        /// <returns>指定对象的集合</returns>
        public List<AutoDetailsDto> FindWithPager(string where, PagerInfo pagerInfo, string fieldToSort, bool desc)
        {
            List<AutoDetails> list = service.FindWithPager(where, pagerInfo, fieldToSort, desc);
            List<AutoDetailsDto> resultList = new List<AutoDetailsDto>();
            foreach (AutoDetails item in list)
            {
                AutoDetailsDto autoDetailsDto = new AutoDetailsDto();
                autoDetailsDto.Id = item.Id;
                autoDetailsDto.ImgUrl =item.ImgUrl;
                autoDetailsDto.MemberId = item.MemberId;
                autoDetailsDto.UserName =serviceMembers.Get(item.MemberId).UserName;
                autoDetailsDto.ShopName =serviceReferrals.GetWhere("MemberId = '"+ item.MemberId + "'").ShopName;
                autoDetailsDto.GoodsCode = item.GoodsCode;
                autoDetailsDto.IsHot = item.IsHot;
                autoDetailsDto.IsOpen = item.IsOpen;
                autoDetailsDto.IsMsg = item.IsMsg;
                autoDetailsDto.IsRed = item.IsRed;
                autoDetailsDto.IsSlide = item.IsSlide;
                autoDetailsDto.IsTop = item.IsTop;
                autoDetailsDto.Author = item.Author;
                autoDetailsDto.BrandId = item.BrandId;
                autoDetailsDto.BrandName = serviceAutoBrand.Get(item.BrandId).Title;
                autoDetailsDto.Click = item.Click;
                autoDetailsDto.CompanyId = item.CompanyId;
                autoDetailsDto.CreatorTime = item.CreatorTime;
                autoDetailsDto.CreatorUserId = item.CreatorUserId;
                autoDetailsDto.DeleteMark = item.DeleteMark;
                autoDetailsDto.DeleteTime = item.DeleteTime;
                autoDetailsDto.DeleteUserId = item.DeleteUserId;
                autoDetailsDto.DeptId = item.DeptId;
                autoDetailsDto.Description = item.Description;
                autoDetailsDto.EnabledMark = item.EnabledMark;
                autoDetailsDto.LastModifyTime = item.LastModifyTime;
                autoDetailsDto.LastModifyUserId = item.LastModifyUserId;
                autoDetailsDto.LikeCount = item.LikeCount;
                autoDetailsDto.LinkUrl = item.LinkUrl;
                autoDetailsDto.SeoDescription = item.SeoDescription;
                autoDetailsDto.SeoKeywords = item.SeoKeywords;
                autoDetailsDto.SeoTitle = item.SeoTitle;
                autoDetailsDto.SortCode = item.SortCode;
                autoDetailsDto.Source = item.Source;
                autoDetailsDto.SubTitle = item.SubTitle;
                autoDetailsDto.Tags = item.Tags;
                autoDetailsDto.Title = item.Title;
                autoDetailsDto.Zhaiyao = item.Zhaiyao;
                autoDetailsDto.MSRP = item.MSRP;
                autoDetailsDto.MonthInstallment = item.MonthInstallment;
                autoDetailsDto.SalesPrice = item.SalesPrice;
                autoDetailsDto.DownPayment = item.DownPayment;
                autoDetailsDto.NumberOfPeriods = item.NumberOfPeriods;
                autoDetailsDto.DownPaymentone = item.DownPaymentone;
                autoDetailsDto.MonthInstallmentone = item.MonthInstallmentone;
                autoDetailsDto.NumberOfPeriods1 = item.NumberOfPeriods1;
                autoDetailsDto.DownPaymenttwo = item.DownPaymenttwo;
                autoDetailsDto.MonthInstallmenttwo = item.MonthInstallmenttwo;
                autoDetailsDto.NumberOfPeriods2 = item.NumberOfPeriods2;
                autoDetailsDto.Preferential = item.Preferential;

                resultList.Add(autoDetailsDto);
            }
            return resultList;
        }

        /// <summary>
        /// 根据推荐属性查询车辆信息
        /// </summary>
        /// <param name="rec">属性值，置顶值为Istop，推荐值为IsRed，热门值为IsHot；幻灯值为IsSlide</param>
        /// <param name="memberId">所属会员ID</param>
        /// <returns></returns>
        public List<AutoDetailsDto> FindCarListByRecommend(string rec,string memberId=null)
        {
            string where = "1=1";
            if (rec == "IsTop")
            {
                where += " and IsTop=1";
            }
            else if (rec == "IsRed")
            {
                where += " and IsRed=1";
            }
            else if (rec == "IsHot")
            {
                where += " and IsHot=1";
            }
            else if (rec == "IsSlide")
            {
                where += " and IsSlide=1";
            }
            if (!string.IsNullOrEmpty(memberId))
            {
                where += string.Format(" and MemberId='{0}' Or IsOpen=1", memberId);
            }
            List<AutoDetails> list = service.GetAllByIsNotDeleteAndEnabledMark(where).OrderBy(t => t.SortCode).ToList();
            List<Albums> albumslist = new List<Albums>();
            List<AutoDetailsDto> resultList = new List<AutoDetailsDto>();
            foreach (AutoDetails item in list)
            {
                AutoDetailsDto autoDetailsDto = new AutoDetailsDto();
                autoDetailsDto.Id = item.Id;
                autoDetailsDto.ImgUrl =item.ImgUrl;
                autoDetailsDto.MemberId = item.MemberId;
                autoDetailsDto.UserName = serviceMembers.Get(item.MemberId).UserName;
                autoDetailsDto.ShopName = serviceReferrals.GetWhere("MemberId = '" + item.MemberId + "'").ShopName;
                autoDetailsDto.GoodsCode = item.GoodsCode;
                autoDetailsDto.IsHot = item.IsHot;
                autoDetailsDto.IsOpen = item.IsOpen;
                autoDetailsDto.IsMsg = item.IsMsg;
                autoDetailsDto.IsRed = item.IsRed;
                autoDetailsDto.IsSlide = item.IsSlide;
                autoDetailsDto.IsTop = item.IsTop;
                autoDetailsDto.Author = item.Author;
                autoDetailsDto.BrandId = item.BrandId;
                autoDetailsDto.BrandName = serviceAutoBrand.Get(item.BrandId).Title;
                autoDetailsDto.Click = item.Click;
                autoDetailsDto.CompanyId = item.CompanyId;
                autoDetailsDto.CreatorTime = item.CreatorTime;
                autoDetailsDto.CreatorUserId = item.CreatorUserId;
                autoDetailsDto.DeleteMark = item.DeleteMark;
                autoDetailsDto.DeleteTime = item.DeleteTime;
                autoDetailsDto.DeleteUserId = item.DeleteUserId;
                autoDetailsDto.DeptId = item.DeptId;
                autoDetailsDto.Description = item.Description;
                autoDetailsDto.EnabledMark = item.EnabledMark;
                autoDetailsDto.LastModifyTime = item.LastModifyTime;
                autoDetailsDto.LastModifyUserId = item.LastModifyUserId;
                autoDetailsDto.LikeCount = item.LikeCount;
                autoDetailsDto.LinkUrl = item.LinkUrl;
                autoDetailsDto.SeoDescription = item.SeoDescription;
                autoDetailsDto.SeoKeywords = item.SeoKeywords;
                autoDetailsDto.SeoTitle = item.SeoTitle;
                autoDetailsDto.SortCode = item.SortCode;
                autoDetailsDto.Source = item.Source;
                autoDetailsDto.SubTitle = item.SubTitle;
                autoDetailsDto.Tags = item.Tags;
                autoDetailsDto.Title = item.Title;
                autoDetailsDto.Zhaiyao = item.Zhaiyao;
                autoDetailsDto.MSRP = item.MSRP;
                autoDetailsDto.MonthInstallment = item.MonthInstallment;
                autoDetailsDto.SalesPrice = item.SalesPrice;
                autoDetailsDto.DownPayment = item.DownPayment;
                autoDetailsDto.NumberOfPeriods = item.NumberOfPeriods;
                autoDetailsDto.DownPaymentone = item.DownPaymentone;
                autoDetailsDto.MonthInstallmentone = item.MonthInstallmentone;
                autoDetailsDto.NumberOfPeriods1 = item.NumberOfPeriods1;
                autoDetailsDto.DownPaymenttwo = item.DownPaymenttwo;
                autoDetailsDto.MonthInstallmenttwo = item.MonthInstallmenttwo;
                autoDetailsDto.NumberOfPeriods2 = item.NumberOfPeriods2;
                autoDetailsDto.Preferential = item.Preferential;

                resultList.Add(autoDetailsDto);
            }
            return resultList;
        }
        /// <summary>
        /// 根据主键获取车型详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AutoDetailsDto GetById(string id)
        {
            AutoDetails item = new AutoDetails();
            AutoDetailsDto autoDetailsDto = new AutoDetailsDto();
            item = service.Get(id);
            autoDetailsDto.Id = item.Id;
            autoDetailsDto.ImgUrl = item.ImgUrl;
            autoDetailsDto.MemberId = item.MemberId;
            autoDetailsDto.UserName = serviceMembers.Get(item.MemberId).UserName;
            autoDetailsDto.ShopName = serviceReferrals.GetWhere("MemberId = '" + item.MemberId + "'").ShopName;
            autoDetailsDto.GoodsCode = item.GoodsCode;
            autoDetailsDto.IsHot = item.IsHot;
            autoDetailsDto.IsOpen = item.IsOpen;
            autoDetailsDto.IsMsg = item.IsMsg;
            autoDetailsDto.IsRed = item.IsRed;
            autoDetailsDto.IsSlide = item.IsSlide;
            autoDetailsDto.IsTop = item.IsTop;
            autoDetailsDto.Author = item.Author;
            autoDetailsDto.BrandId = item.BrandId;
            autoDetailsDto.BrandName = serviceAutoBrand.Get(item.BrandId).Title;
            autoDetailsDto.Click = item.Click;
            autoDetailsDto.CompanyId = item.CompanyId;
            autoDetailsDto.CreatorTime = item.CreatorTime;
            autoDetailsDto.CreatorUserId = item.CreatorUserId;
            autoDetailsDto.DeleteMark = item.DeleteMark;
            autoDetailsDto.DeleteTime = item.DeleteTime;
            autoDetailsDto.DeleteUserId = item.DeleteUserId;
            autoDetailsDto.DeptId = item.DeptId;
            autoDetailsDto.Description = item.Description;
            autoDetailsDto.EnabledMark = item.EnabledMark;
            autoDetailsDto.LastModifyTime = item.LastModifyTime;
            autoDetailsDto.LastModifyUserId = item.LastModifyUserId;
            autoDetailsDto.LikeCount = item.LikeCount;
            autoDetailsDto.LinkUrl = item.LinkUrl;
            autoDetailsDto.SeoDescription = item.SeoDescription;
            autoDetailsDto.SeoKeywords = item.SeoKeywords;
            autoDetailsDto.SeoTitle = item.SeoTitle;
            autoDetailsDto.SortCode = item.SortCode;
            autoDetailsDto.Source = item.Source;
            autoDetailsDto.SubTitle = item.SubTitle;
            autoDetailsDto.Tags = item.Tags;
            autoDetailsDto.Title = item.Title;
            autoDetailsDto.Zhaiyao = item.Zhaiyao;
            autoDetailsDto.MSRP = item.MSRP;
            autoDetailsDto.MonthInstallment = item.MonthInstallment;
            autoDetailsDto.SalesPrice = item.SalesPrice;
            autoDetailsDto.DownPayment = item.DownPayment;
            autoDetailsDto.NumberOfPeriods = item.NumberOfPeriods;
            autoDetailsDto.DownPaymentone = item.DownPaymentone;
            autoDetailsDto.MonthInstallmentone = item.MonthInstallmentone;
            autoDetailsDto.NumberOfPeriods1 = item.NumberOfPeriods1;
            autoDetailsDto.DownPaymenttwo = item.DownPaymenttwo;
            autoDetailsDto.MonthInstallmenttwo = item.MonthInstallmenttwo;
            autoDetailsDto.NumberOfPeriods2 = item.NumberOfPeriods2;
            autoDetailsDto.Preferential = item.Preferential;
            string sqlWhere = string.Format("Channel='Car' and ContentId='{0}'", item.Id);
            IEnumerable<Albums> ListAlbums = albumsService.GetListWhere(sqlWhere);
            autoDetailsDto.ListAlbums = ListAlbums;

            return autoDetailsDto;
        }

        /// <summary>
        /// 根据品牌id查询是否存在车型
        /// </summary>
        public int GetWhereByBrandid(string Brandid)
        {
            AutoDetails autoDetails = service.GetWhere(string.Format("BrandId='{0}'", Brandid));
            if (autoDetails != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
